<?php
/**
 * Created by PhpStorm.
 * User: leo
 * Date: 16/4/11
 * Time: 下午1:22
 * Describe:
 */

namespace common\components\redis;


class Connection extends \yii\redis\Connection
{
    public static $myRedis           = [];
    public        $connectionTimeout = 20;
    public        $prefix            = '';
    public        $redis             = null;

    public function open()
    {
        $key = $this->hostname . $this->port;

        /*if (!array_key_exists($key, static::$myRedis)) {
            self::$myRedis[$key] = new \Redis();
            self::$myRedis[$key]->connect($this->hostname, $this->port, $this->connectionTimeout);
            if (!empty($this->prefix)) {
                $this->redis->setOption(\Redis::OPT_PREFIX, $this->prefix);
            }
        }
        $this->redis = &self::$myRedis[$key];*/


        if (!(self::$myRedis instanceof \Redis)) {
            if ($this->port)
                self::$myRedis = new \Redis();
        }
        self::$myRedis->connect($this->hostname, $this->port, $this->connectionTimeout);
        if (!empty($this->prefix)) {
            self::$myRedis->setOption(\Redis::OPT_PREFIX, $this->prefix);
        }
        $this->redis = &self::$myRedis;

    }

    public function executeCommand($name, $params = [])
    {
        $this->open();
        return call_user_func_array([$this->redis, $name], $params);

    }
}